package com.oscar.ehome.base.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;

import com.oscar.ehome.base.AppManager;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.view.LoadingDialog;
import com.oscar.ehome.R;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by on 2017/5/22 10:44.
 *
 * @author mengwanhe
 */

public abstract class BasesActivity<V, T extends BasePresenter> extends FragmentActivity {
    private boolean flag;
    //加载框
    private LoadingDialog loadingDialog;

    AppManager appManager;
    protected T mPresenter;//Presenter对象


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            CommonUtils.setBaseBackground(BasesActivity.this, true);


        appManager = AppManager.getAppManager();
        appManager.addActivity(this);

        mPresenter = createPresenter();//创建Presenter
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
    }


    @Override
    public void setContentView(int layoutResID) {

        ViewGroup viewGroup =  getWindow().getDecorView()
                .findViewById(android.R.id.content);
        View oriView = getLayoutInflater().inflate(layoutResID, viewGroup);
        View view = getLayoutInflater().inflate(R.layout.base_common_loading,
                viewGroup);

    }

    protected void showLoading() {
        showLoading(false);
        flag = false;


    }

    protected void showLoading(boolean canceledOnTouchOutside) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show(canceledOnTouchOutside);
        }
    }

    protected void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart(AppManager.getPackageName(this));
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(AppManager.getPackageName(this));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    protected abstract T createPresenter();

}
